//
//  SharingViewController.m
//  PerfectWorld
//
//  Created by dllo on 16/9/19.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "SharingViewController.h"
#import "SharingTableViewCell.h"
#import "AFNetworking.h"
#import "SharingModel.h"
#import "SharingPhotosModel.h"
#import "SharingSonViewController.h"

@interface SharingViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)SharingModel *model;
@end

@implementation SharingViewController
/**
 *  分享
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self creatTableView];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 50)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"SharingTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
}
- (void)handleData {
    NSString *nameStr = [_urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *footStr = [@"木木" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *strURL = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/%@/photos?start=0&count=10&user=%@", nameStr, footStr];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:strURL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        self.model = [SharingModel modelWithDic:dic];
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error = %@", error);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _model.photos.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SharingSonViewController *sonVC = [[SharingSonViewController alloc] init];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:_model.photos];
    sonVC.sharingArr = arr;
    sonVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:sonVC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.frame.size.height / 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SharingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.photos = _model.photos[indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
